
#include "MainMenuState.h"
#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"
#include "GameplayState.h"
#include "OptionsState.h"
#include "CreditsState.h"
#include "LoadState.h"
#include "HowToPlayState.h"
#include "Game.h"
#include "BitmapFont.h"
#include <cassert>
#include "tinyxml.h"


//Singleton accessor
MainMenuState* MainMenuState::GetInstance(void)
{
	static MainMenuState instance;
	return &instance;
}


//Constructor
MainMenuState::MainMenuState(void)
{
	m_nCursor = 0;
}


//Destructor
MainMenuState::~MainMenuState(void)
{
}


//Enter
void MainMenuState::Enter(void)
{
	mouseDelay = 0.5f;
	LoadSoundOptions();
	menuSoundID = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("../resource/sounds/PlaceholderSound.wav"));
}


//Exit
void MainMenuState::Exit (void)
{
	CSGD_XAudio2::GetInstance()->SFXStopSound(menuSoundID);
	CSGD_XAudio2::GetInstance()->SFXUnloadSound(menuSoundID);
	menuSoundID = -1;
}


//Input
bool MainMenuState::Input(void)
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	static int lastMouseY = 0;

	//Ignore input when alt is down
	if (pDI->KeyDown(DIK_LALT) == true || pDI->KeyDown(DIK_RALT) == true)
		return true;

	if (pDI->KeyPressed(DIK_ESCAPE) || pDI->MouseButtonPressed(1))
	{
		if (m_nCursor == MENU_EXIT)
			return false;
		else
		{
			CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);
			m_nCursor = MENU_EXIT;
			lastMouseY = pDI->MouseGetPosY();
		}
	}

	if (pDI->KeyPressed(DIK_UP) == true)
	{
		m_nCursor--;
		if( m_nCursor < 0 )
			m_nCursor = MENU_ITEMCOUNT-1;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);
		lastMouseY = pDI->MouseGetPosY();
	}
	else if (pDI->KeyPressed(DIK_DOWN) == true)
	{
		m_nCursor++;
		if( m_nCursor >= MENU_ITEMCOUNT)
			m_nCursor = 0;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);
		lastMouseY = pDI->MouseGetPosY();
	}


	if (pDI->KeyPressed(DIK_RETURN) || pDI->MouseButtonPressed(0))
	{
		switch (m_nCursor)
		{
		case MENU_PLAY:
			{
				Game::GetInstance()->ChangeState(LoadState::GetInstance());
				return true;
			}
			break;

		case MENU_OPTIONS:
			{
				Game::GetInstance()->ChangeState(OptionsState::GetInstance());
				return true;
			}
			break;

		case MENU_HOWTOPLAY:
			{
				Game::GetInstance()->ChangeState(HowToPlayState::GetInstance());
				return true;
			}
			break;

		case MENU_CREDITS:
			{
				Game::GetInstance()->ChangeState(CreditsState::GetInstance());
				return true;
			}
			break;

		case MENU_EXIT:
			{
				return false; //Exit game
			}
			break;

		default:
			m_nCursor = 0;
		}
	}

	int lastCursor = m_nCursor;
	int y = pDI->MouseGetPosY();
	if (y != lastMouseY && mouseDelay <= 0.0f)
	{
		lastMouseY = 0;
		int initialY = 310;
		int currentY = initialY;
		int offsetY = 35;
		if (y > currentY && y < currentY+offsetY) 
			m_nCursor = MENU_PLAY;
		currentY += offsetY;
		if (y > currentY && y < currentY+offsetY) 
			m_nCursor = MENU_OPTIONS;
		currentY += offsetY;
		if (y > currentY && y < currentY+offsetY) 
			m_nCursor = MENU_HOWTOPLAY;
		currentY += offsetY;
		if (y > currentY && y < currentY+offsetY) 
			m_nCursor = MENU_CREDITS;
		currentY += offsetY;
		if (y > currentY && y < currentY+offsetY) 
			m_nCursor = MENU_EXIT;
		currentY += offsetY;
	}
	else
		lastMouseY = pDI->MouseGetPosY();

	if (m_nCursor != lastCursor)
		CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);

	return true; //Keep running the game
}


//Update
void MainMenuState::Update(float dt)
{
	if (mouseDelay > 0.0f)
		mouseDelay -= dt;
}


//Render
void MainMenuState::Render(void)
{
	//Game title
	CBitmapFont* pFont = Game::GetInstance()->GetFont();
	pFont->Draw( _T("Heavy Reinforcements"), 60, 170, 2.0f, 
		D3DCOLOR_ARGB(255, 255, 255, 255));

	int initialY = 310;
	int currentY = initialY;
	int offsetY = 35;
	DWORD color = D3DCOLOR_ARGB( 255, 255, 255, 255);

	//Menu options
	pFont->Draw(_T("Play"), 330, currentY, 1.0f, color);
	currentY += offsetY;

	pFont->Draw(_T("Options"), 330, currentY, 1.0f, color);
	currentY += offsetY;

	pFont->Draw(_T("How to play"), 330, currentY, 1.0f, color);
	currentY += offsetY;

	pFont->Draw(_T("Credits"), 330, currentY, 1.0f, color);
	currentY += offsetY;

	pFont->Draw(_T("Quit"), 330, currentY, 1.0f, color);
	currentY += offsetY;

	//Menu cursor
	pFont->Draw(_T(">"), 290, initialY + (offsetY * m_nCursor), 1.0f, color);
}


//Load sound options
void MainMenuState::LoadSoundOptions(void) const
{
	CSGD_XAudio2* XA = CSGD_XAudio2::GetInstance();

	TiXmlDocument document;
	if (document.LoadFile("SoundOptions.xml") == false)
		return;
	TiXmlElement* root = document.RootElement();
	if (root == nullptr)
		return;
	TiXmlElement* element;
	element = root->FirstChildElement("Sound");
	int soundVolumePercent;
	element->Attribute("VolumePercent", &soundVolumePercent);

	element = root->FirstChildElement("Music");
	int musicVolumePercent;
	element->Attribute("VolumePercent", &musicVolumePercent);

	if (soundVolumePercent > 100)
		soundVolumePercent = 100;
	if (soundVolumePercent < 0)
		soundVolumePercent = 0;
	if (musicVolumePercent > 100)
		musicVolumePercent = 100;
	if (musicVolumePercent < 0)
		musicVolumePercent = 0;

	XA->SFXSetMasterVolume(soundVolumePercent*0.01f);
	XA->MusicSetMasterVolume(musicVolumePercent*0.01f);
}
